package persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AdmPersistenciaJerarquia_Rol {
	
	private static AdmPersistenciaJerarquia_Rol instancia;
	
	public static AdmPersistenciaJerarquia_Rol getInstancia(){
		if(instancia == null)
			return new AdmPersistenciaJerarquia_Rol();
		return instancia;
	}

	public String[] selectAllJer() {
		System.out.println("LOG: AdmPersistenciaJerarquia_Rol / selectAllJer [INICIO]");
		String[]jer = new String[20];
		int i = 0;
		try{
			
			Connection con = ConectorSQL.conectar();  
			PreparedStatement ps = con.prepareStatement("select nombre from jerarquia");
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				jer[i] = rs.getString(1);
				i++;
			}
			con.close();
			System.out.println("LOG: AdmPersistenciaJerarquia_Rol / selectAllJer [FIN]");
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return jer;
	}
	
	public String[] selectAllRol() {
		System.out.println("LOG: AdmPersistenciaJerarquia_Rol / selectAllRol [INICIO]");
		String[]rol = new String[20];
		int i = 0;
		try{
			
			Connection con = ConectorSQL.conectar();  
			PreparedStatement ps = con.prepareStatement("select nombre from rol");
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				rol[i] = rs.getString(1);
				i++;
			}
			con.close();
			System.out.println("LOG: AdmPersistenciaJerarquia_Rol / selectAllRol [FIN]");
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return rol;
	}

	public long getIdJer(String jerarquia) {
		System.out.println("LOG: AdmPersistenciaJerarquia_Rol / getIdJer [INICIO]");
		try{
			
			Connection con = ConectorSQL.conectar();  
			PreparedStatement ps = con.prepareStatement("select id_jer from jerarquia where nombre = ?");
			ps.setString(1, jerarquia);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				return rs.getLong(1);
			}
			con.close();
			System.out.println("LOG: AdmPersistenciaJerarquia_Rol / getIdJer [FIN]");
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return -1;
	}

	public long getIdRol(String rol) {
		System.out.println("LOG: AdmPersistenciaJerarquia_Rol / getIdRol [INICIO]");
		try{
			
			Connection con = ConectorSQL.conectar();  
			PreparedStatement ps = con.prepareStatement("select id_rol from rol where nombre = ?");
			ps.setString(1, rol);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				return rs.getLong(1);
			}
			con.close();
			System.out.println("LOG: AdmPersistenciaJerarquia_Rol / getIdRol [FIN]");
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return -1;
	}
	
	
}
